package com.android.camera.animation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;

public class AnimationFadeOut extends Animation {

    private float mAlpha = 0.0f;

    public AnimationFadeOut(View view) {
        mView = view;
        mInterpolator = new AccelerateDecelerateInterpolator();
        mDuration = Animation.DURATION_DEFAULT;
        mDelay = 0;
        mListener = null;
    }

    @Override
    public void animate() {
        final float originalAlpha = mView.getAlpha();
        AnimatorSet fadeSet = new AnimatorSet();
        fadeSet.play(ObjectAnimator.ofFloat(mView, View.ALPHA, mAlpha));
        fadeSet.setInterpolator(mInterpolator);
        fadeSet.setDuration(mDuration);
        fadeSet.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationStart(Animator animation) {
                if (getListener() != null) {
                    getListener().onAnimationStart(AnimationFadeOut.this);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (mAlpha == 0.0f) {
                    mView.setVisibility(View.INVISIBLE);
                    mView.setAlpha(originalAlpha);
                }
                if (getListener() != null) {
                    getListener().onAnimationEnd(AnimationFadeOut.this);
                }
            }
        });
        fadeSet.setStartDelay(mDelay);
        fadeSet.start();
    }

    public TimeInterpolator getInterpolator() {
        return mInterpolator;
    }

    public AnimationFadeOut setInterpolator(TimeInterpolator interpolator) {
        mInterpolator = interpolator;
        return this;
    }

    public long getDuration() {
        return mDuration;
    }

    public AnimationFadeOut setDuration(long duration) {
        mDuration = duration;
        return this;
    }

    public long getDelay() {
        return mDelay;
    }

    public AnimationFadeOut setDelay(int delay) {
        mDelay = delay;
        return this;
    }

    public AnimationListener getListener() {
        return mListener;
    }

    public AnimationFadeOut setListener(AnimationListener listener) {
        mListener = listener;
        return this;
    }

    public float getAlpha() {
        return mAlpha;
    }

    public AnimationFadeOut setAlpha(float alpha) {
        mAlpha = alpha;
        return this;
    }
}
